package com.olda.play.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.olda.play.enums.ConstantEnum;
import com.olda.play.enums.ResponseCode;
import com.olda.play.model.PageResp;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysDictionaryBizService;
import com.olda.play.system.biz.SysDictionaryItemBizService;
import com.olda.play.system.biz.dao.SysDictionaryBizMapper;
import com.olda.play.system.dto.SysDictionaryDto;
import com.olda.play.system.model.dao.SysDictionaryMapper;
import com.olda.play.system.model.po.SysDictionary;
import com.olda.play.system.model.po.SysDictionaryItem;
import com.olda.play.system.service.SysDictionaryService;
import com.olda.play.system.vo.SysDictionaryVo;
import com.olda.play.utils.UserUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 数据字典接口
 *  查询、新增、删除、编辑
 *
 * @author oldA
 *
 */
@Service
@RequestMapping(value =  "/dict")
@RestController
public class SysDictionaryServiceImpl implements SysDictionaryService {

    @Autowired
    private SysDictionaryBizMapper sysDictionaryBizMapper;
    @Autowired
    private SysDictionaryMapper sysDictionaryMapper;

    @Autowired
    private SysDictionaryBizService sysDictionaryBizService;
    @Autowired
    private SysDictionaryItemBizService sysDictionaryItemBizService;

    /**
     * 数据字典分列表分页查询
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysDictionaryList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto querySysDictionaryList(@RequestBody SysDictionaryVo param) {
        Map<String,Object> map=BeanUtil.copyProperties(param, Map.class);
        map.put("isDeleted",ConstantEnum.IS_DELETE_NO.getCode());
        int pageNum = param.getPageNum();
        int pageSize = param.getPageSize();
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<SysDictionaryDto> list=sysDictionaryBizMapper.selectByCondition(map);
        PageInfo<SysDictionaryDto> pageInfo = new PageInfo(list);
        int count = Integer.parseInt(String.valueOf(pageInfo.getTotal()));
        if (CollectionUtils.isEmpty(list)) {
//            return CommonResponseDto.success(PageResp.create(Collections.EMPTY_LIST, count, pageNum, pageSize));
            return CommonResponseDto.success(new PageInfo<>(new ArrayList<>()));
        }
//        return CommonResponseDto.success(PageResp.create(list, count, pageNum, pageSize));
        return CommonResponseDto.success(pageInfo);
    }

    /**
     * 根据id查询数据字典最新详情
     * @param id
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysDictDetailById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<SysDictionaryDto> querySysDictDetailById(@RequestParam Long id) {
        if(id==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        SysDictionary sysDictionary=sysDictionaryBizService.selectSysDictById(id);
        return CommonResponseDto.success(BeanUtil.copyProperties(sysDictionary,SysDictionaryDto.class));
    }

    /**
     * 新增或修改数据字典
     * @param param
     * @return
     */
    @Override
    @RequestMapping(value = "/addOrUpdateSysDict",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> addOrUpdateSysDict(@RequestBody SysDictionaryVo param) {
        if(param==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Long id=param.getId();
        if(id==null){
            //新增-- 注意判断业务字段编码是否重复
            SysDictionary sysDictionaryOld=sysDictionaryBizService.selectSysDictBydictCode(param.getDictCode());
            if(sysDictionaryOld!=null){
                return CommonResponseDto.error(ResponseCode.MORE_DICT);
            }
            SysDictionary sysDictionary=BeanUtil.copyProperties(param,SysDictionary.class);
            sysDictionary.setCreateTime(new Date());
            sysDictionary.setCreateUser(UserUtils.getUserId().toString());
            sysDictionary.setCreateUserName(UserUtils.getUsername());
            sysDictionary.setIsDeleted(ConstantEnum.IS_DELETE_NO.getCode());
            sysDictionary.setPlatform(ConstantEnum.PLATEFORM_0.getCode());
            sysDictionaryMapper.insert(sysDictionary);
        }else{
            SysDictionary old=sysDictionaryBizService.selectSysDictById(id);

            SysDictionary sysDictionary=BeanUtil.copyProperties(param,SysDictionary.class);
            sysDictionaryBizService.updateSysDict(sysDictionary);
            //修改--要注意业务字典编码是否修改-如果修改要同步修改数据字典子项表
            if(!old.getDictCode().equals(sysDictionary.getDictCode())){
                SysDictionaryItem sysDictionaryItem=new SysDictionaryItem();
                sysDictionaryItem.setDictCode(sysDictionary.getDictCode());
                sysDictionaryItemBizService.updateSysDictItemByDictCode(sysDictionaryItem,old.getDictCode());
            }
        }
        return CommonResponseDto.success(true);
    }

    /**
     * 删除数据字典
     * 仅支持单条删除且需要二次确认弹窗
     * @param id
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteSysDictById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> deleteSysDictById(@RequestParam Long id) {
        if(id==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        SysDictionary sysDictionary=sysDictionaryBizService.selectSysDictById(id);
        if(sysDictionary==null){
            return CommonResponseDto.error(ResponseCode.HAS_NO_DATA);
        }
        sysDictionaryBizService.deleteSysDictBydictCode(sysDictionary.getDictCode());
        sysDictionaryItemBizService.deleteSysDictItemBydictCode(sysDictionary.getDictCode());
        return CommonResponseDto.success(true);
    }
}
